<?php
declare(strict_types=1);

include_once './vendor/autoload.php';

use Swow\Coroutine;
use Swow\Sync\WaitGroup;

function work3(int $id): void
{
    for ($n = 0; $n++ < 3;) {
        echo "Do something...\n";
        sleep(1);
        echo sprintf("Worker[%d] Task#%d done\n", $id, $n);
    }
    echo sprintf("Worker[%d] exit\n", $id);
}

$wg = new WaitGroup();
// $wg->add(3); // 这么写也可以
for ($c = 0; $c < 3; $c++) {
    $wg->add();
    Coroutine::run(static function () use ($c, $wg): void {
        work3($c);
        $wg->done();
    });
}

echo "Wait...\n";
$wg->wait();
echo "Done\n";
